home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / borland / jnfb88.zip / TSORT.ZIP / UNIVDIR.PRO < prev   
Text File  |  1987-10-20  |  6KB  |  207 lines

  1.  
  2. /*   UNIVDIR.PRO
  3.  
  4.      This routine collects a database of all files in
  5.      all subdirectories on a disk, sorts the filenames, and
  6.      then uses the result to output a formatted, sorted list of
  7.      files to the screen and an ASCII file.
  8.  
  9.      Copyright 1987, by Alex Lane
  10.  
  11.      This program uses modified routines from the Turbo Prolog
  12.      Toolbox, which is Copyright 1987, Borland International.
  13.  
  14. */
  15.  
  16.  
  17. domains
  18.    file = f1; f2
  19.    charlist = char *
  20.  
  21. database
  22.  
  23.    file(string,string,integer,real,string,integer,integer,real,
  24.         integer,integer,integer)
  25.    path(string) /* used to build a pseudo-stack of subdirectories */
  26.    t(integer)  /* this functor provides a 'tag' identification */
  27.  
  28. predicates
  29.    univdir(string)
  30.    post_or_record(string,integer,integer,integer,real,integer,
  31.                   integer,real,string)
  32.    tag(integer)
  33.    tokenize_filename(string,string,string)
  34.    list_text(string,charlist)     /* (i,o) */
  35.  
  36.    list_text1(string,charlist,string)
  37.    append(charlist,charlist,charlist)
  38.    pretty_out(file,file)
  39.    repeat
  40.  
  41. goal    
  42.    makewindow(1,7,7,"Hard disk directory list sorter",0,0,25,80),
  43.    asserta(t(1)),  /* initialize the tag index to 1 */
  44.  
  45. /* The file TEST.DAT will contain 18-byte records consisting of
  46.    a 12-byte file name, a 4-digit tag identification, and a
  47.    carriage-return-line-feed comhbination
  48. */
  49.    openwrite(f1,"TEST.DAT"), 
  50.         
  51. /* the argument to univdir() specifies where to START the
  52.    directory search.  Examples:
  53.    "" - searches the default disk
  54.    "C:" - searches the C: disk (typically the hard disk)
  55.    "\\PROLOG" - searches the subdirectory \PROLOG on the
  56.                 default disk (recall that the '\' character
  57.                 must appear twice in a string since it is the
  58.                 escape character).
  59. */
  60.    univdir("C:"),
  61.    closefile(f1),
  62.    retract(t(_)),  /* this fella's job is done */
  63.         
  64. /* we now make a system call to DOS, which will run the
  65.    program RDSRT.COM for us.  RDSRT.COM already expects to find
  66.    a file called TEST.DAT in the default directory, and after
  67.    processing, will leave a file called TEST.OUT for us to use
  68.    when control returns to this program. 
  69. */
  70.    system("RDSRT"),
  71.    clearwindow,
  72.    openread(f1,"TEST.OUT"),
  73.  
  74. /* we will write the sorted hard disk file information to the
  75.    file MYFILES.TXT on the default disk.
  76. */
  77.    openwrite(f2,"MYFILES.DAT"),
  78.    readdevice(f1),
  79.    writedevice(f2),
  80.    pretty_out(f1,f2),
  81.    readdevice(keyboard),
  82.    writedevice(screen), /* restore the console devices for i/o */
  83.    closefile(f1),
  84.    closefile(f2),
  85.    deletefile("TEST.DAT"),
  86.    deletefile("TEST.OUT"), /* clean up a little before exiting */
  87.    write("\n\nDone. The sorted file listing is in MYFILES.DAT").
  88.  
  89. /* FINDMATC.PRO is a modified excerpt of the Turbo Toolbox file
  90.    BIOS.PRO Modifications to support file sizes greater 32767.
  91. */
  92. include "findmatc.pro"
  93.  
  94. clauses
  95.  
  96. /* The univdir() predicate is the workhorse of the the program.  It 
  97.    calls the Toolbox predicate findmatch() to retrieve filenames. If 
  98.    the file name is the name of a subdirectory, the predicate 
  99.    post_or_record() asserts the subdirectory name using the path() 
  100.    functor. Otherwise, the filename is prepared for sorting and 
  101.    asserted through the file() functor.
  102. */   
  103.    univdir(Path) :-
  104.       concat(Path,"\\*.*",SearchSpec),
  105.       findmatch(SearchSpec,63, 
  106.                 Filename,FileAttr,FileH,FileM,FileY, 
  107.                 FileMo,FileD,FileSize),
  108.       Filename <> ".",  /* ignore the DOS . and .. file entries */
  109.       Filename <> "..",
  110.       post_or_record(Filename,FileAttr,FileH,FileM,FileY,FileMo,
  111.                      FileD,FileSize,Path),
  112.       fail.  /* we use fail here to force backtracking to
  113.                 findmatch() */
  114.  
  115. /* Once we've exhausted the entries in any given directory, this call
  116.    to univdir() sets us down another path to a new subdirectory by
  117.    retracting a path name through the path() functor.
  118. */
  119.    univdir(_):-
  120.       retract(path(NewPath)),
  121.       write("!"),
  122.       univdir(NewPath).
  123.  
  124. /* if we've run out of entries and out of path names, we're done 
  125.    scanning the directory on the disk.
  126. */      
  127.    univdir(_):- !.       
  128.  
  129.    post_or_record(Name,16,_,_,_,_,_,_,Path):-
  130.       concat(Path,"\\",Halfway),
  131.       concat(Halfway,Name,NewPath),
  132.       asserta(path(NewPath)).
  133.    post_or_record(Name,Attr,Hour,Min,Year,Mo,Day,Size,Path):-  
  134.       Attr <> 16,
  135.       write("#"),
  136.       tokenize_filename(Name,File,Ext),
  137.       writedevice(f1),
  138.       tag(A),
  139.       writef("%-8%-4%4\n", File,Ext,A),
  140.       writedevice(screen),
  141.       assertz(file(File,Ext,Attr,Size,Path,Hour,Min,Year,Mo,Day,A)),
  142.       !.
  143.         
  144. /*  I'd like to extract the file and extension parts of the name so
  145.     as to be able to uniformly sort on an 8-char file and 3-char
  146.     extension, with both the file and extension flush left. In other
  147.     words, instead of having names like:
  148.         C.BAT
  149.         FOO.BAR
  150.         DISKDOPE.C
  151.         README
  152.     I'd like to have:
  153.         C       .BAT
  154.         FOO     .BAR
  155.         DISKDOPE.C
  156.         README  .
  157. */
  158.    tokenize_filename(Name,File,Ext) :-
  159.       list_text(Name,Namelist),
  160.       append(A,['.'|T],Namelist),
  161.       B = [ '.' | T ],
  162.       list_text(File,A),
  163.       list_text(Ext,B).
  164.  
  165.    tokenize_filename(Name,Name,".").
  166.  
  167.    list_text("",[]) :- !.
  168.    list_text(String,[H|T]) :-
  169.       bound(String),
  170.       frontchar(String,H,Rest),
  171.       list_text(Rest,T).
  172.    list_text(String,[H|T]) :-
  173.       bound(H),
  174.       list_text1("",[H|T],String).
  175.  
  176.    list_text1(A,[],A) :- !.
  177.    list_text1(A,[H|T],Out) :-
  178.       str_char(Hs,H),
  179.       concat(A,Hs,AHs),
  180.       list_text1(AHs,T,Out).
  181.         
  182.    tag(N) :-
  183.       retract(t(A)),
  184.       N = A + 1,
  185.       asserta(t(N)), ! .
  186.  
  187.    append([],L,L).
  188.    append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).           
  189.  
  190.  
  191. /* The pretty_out() predicate lets me output a nicely formatted list
  192.    of file information.
  193. */   
  194.    pretty_out(In,Out) :-
  195.       repeat,
  196.       readint(A),
  197.       retract(file(File,Ext,_,Size,Path,Hour,Min,Year,Mo,Day,A)),
  198.       writef("%-8%-4  %6.0f   %02:%02   %02-%02-%4   %-24\n",
  199.               File,Ext,Size,Hour,Min,Mo,Day,Year,Path),
  200.       writedevice(screen),
  201.       write("%"),
  202.       writedevice(Out),
  203.       eof(In).
  204.  
  205.    repeat.
  206.    repeat:- repeat.
  207.